package com.android.settings.ios.view;

import android.content.Context;
import android.util.AttributeSet;

import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;

public class OsPreferenceCategory extends PreferenceCategory {

    public OsPreferenceCategory(Context context) {
        super(context);
    }

    public OsPreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public OsPreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public OsPreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void onAttached() {
        super.onAttached();
        updateItemBackground();
    }

    public void updateItemBackground() {
        final int count = getPreferenceCount();
        if (count == 1) {
            Preference preference = getPreference(0);
            if (preference instanceof IOsPreference) {
                ((IOsPreference) preference).setItemBackgroundMode(OsPreference.MODE_SINGLE);
            }
        } else if (count > 1) {
            for (int i = 0; i < count; i++) {
                Preference preference = getPreference(i);
                if (preference instanceof IOsPreference) {
                    if (i == 0) {
                        ((IOsPreference) preference).setItemBackgroundMode(OsPreference.MODE_TOP);
                    } else if (i == count - 1) {
                        ((IOsPreference) preference).setItemBackgroundMode(OsPreference.MODE_BOTTOM);
                    } else {
                        ((IOsPreference) preference).setItemBackgroundMode(OsPreference.MODE_NONE);
                    }
                }
            }
        }
    }
}
